package com.py.framework.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.utils.DateUtils;
import com.py.framework.dao.OfficeDao;
import com.py.framework.entity.OfficeEntity;
import com.py.framework.service.OfficeService;
import com.py.framework.service.RoleService;
import com.py.framework.utils.UuidUtil;

/**
 * 部门管理Service实现
 * @author Leegern
 * @date   2018年4月25日
 */
@Service
public class OfficeServiceImpl implements OfficeService {
	
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private RoleService roleService;
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#addOfficeInfo(com.py.framework.entity.OfficeEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addOfficeInfo(OfficeEntity entity) {
		if (null != entity ) {
			if (entity.getParentId() > Constants.NUMBER_ZERO) {
				OfficeEntity office = this.findOneOffice(entity.getParentId());
				if (null != office && StringUtils.isNotBlank(office.getAllParentId())) {
					entity.setAllParentId(office.getAllParentId() + "," + entity.getParentId());
				} else {
					entity.setAllParentId(String.valueOf(entity.getParentId()));
				}
			}
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			entity.setDelFlag(Constants.NUMBER_ONE);
			if(entity.getParentId() == 0) {
				entity.setLhjcToken(UuidUtil.getInstance().getUUid());
			}
			return officeDao.addOfficeInfo(entity);
		}
		return 0;
	}

	@Override
	public int getAreaIdByDeptId(String id) {
		return officeDao.getAreaIdByDeptId(id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#queryOfficeForPage(java.util.Map)
	 */
	@Override
	public List<OfficeEntity> queryOfficeForPage(Map<String, Object> params) {
		return officeDao.queryOfficeForPage(params);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getOfficesByParentId(java.lang.String)
	 */
	@Override
	public List<OfficeEntity> getOfficesByOfficeId(String officeId) {
		return officeDao.getOfficesByOfficeId(officeId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#findOneOffice(long)
	 */
	@Override
	public OfficeEntity findOneOffice(long id) {
		if (id > Constants.NUMBER_ZERO) {
			return officeDao.findOneOffice(id);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#updateOffice(com.py.framework.entity.OfficeEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateOffice(OfficeEntity entity) {
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		return officeDao.updateOffice(entity);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#batchDelOffice(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int batchDelOffice(List<Long> ids) {
		int flag = 0;
		if (! CollectionUtils.isEmpty(ids)) {
			List<OfficeEntity> datas = null;
			for (Long id : ids) {
				datas = this.getOfficesByOfficeId(String.valueOf(id));
				if (! CollectionUtils.isEmpty(datas)) {
					throw new ServiceException("存在子节点，删除失败！");
				}
			}
			Map<String, Object> params = new HashMap<>();
			params.put("ids", ids);
			params.put("updateTime", new Timestamp(DateUtils.getCurrentDateTime().getTime()));
			flag = officeDao.batchDelOffice(params);
			// 删除角色部门信息
			for (Long officeId : ids) {
				params.clear();
				params.put("officeId", officeId);
				roleService.delRoleOfficeRelate(params);
			}
		}
		return flag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getOfficesByCode(long, java.lang.String)
	 */
	@Override
	public List<OfficeEntity> getOfficesByCode(long officeId, String officeCode) {
		return officeDao.getOfficesByCode(officeId, officeCode);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getUserOffice(long)
	 */
	@Override
	public OfficeEntity getUserOffice(long userId) {
		if (userId > Constants.NUMBER_ZERO) {
			return officeDao.getUserOffice(userId);
		}
		return null;
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getOfficesByPid(long)
	 */
	@Override
	public List<OfficeEntity> getOfficesByPid(long parentId) {
		return officeDao.getOfficesByPid(parentId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getOfficesByAreaId(long)
	 */
	@Override
	public List<OfficeEntity> getOfficesByAreaId(long areaId) {
		if (areaId > Constants.NUMBER_ZERO) {
			return officeDao.getOfficesByAreaId(areaId);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getRootOffices()
	 */
	@Override
	public List<OfficeEntity> getRootOffices() {
		return officeDao.getRootOffices();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getAllOffices()
	 */
	@Override
	public List<OfficeEntity> getAllOffices() {
		return officeDao.getAllOffices();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getOfficesByParentId(java.lang.String)
	 */
	@Override
	public List<OfficeEntity> getOfficesByParentId(String parentId) {
		if (StringUtils.isNotBlank(parentId)) {
			return officeDao.getOfficesByParentId(parentId);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.OfficeService#getSubOfficesByAreaId(long)
	 */
	@Override
	public List<OfficeEntity> getSubOfficesByAreaId(long areaId) {
		List<OfficeEntity> result = new ArrayList<>();
		List<OfficeEntity> items = null;
		if (areaId > Constants.NUMBER_ZERO) {
			List<OfficeEntity> offices = this.getOfficesByAreaId(areaId);
			if (! CollectionUtils.isEmpty(offices)) {
				// 防止重复
				Set<Long> ids = new HashSet<>();
				for (OfficeEntity officeEntity : offices) {
					if (null != officeEntity && officeEntity.getId() > Constants.NUMBER_ZERO) {
						if (!ids.contains(officeEntity.getId())) {
							result.add(officeEntity);
							ids.add(officeEntity.getId());
						}
						// 获取区域部门下的子部门
						items = this.getOfficesByParentId(String.valueOf(officeEntity.getId()));
						if (! CollectionUtils.isEmpty(items)) {
							for (OfficeEntity item : items) {
								// 去重添加
								if (null != item && !ids.contains(item.getId())) {
									ids.add(item.getId());
									result.add(item);
								}
							}
						}
					}
				}
			}
		}
		return result;
	}

	@Override
	public void generateOfficeToken() {
		List<Long> officeIdList = officeDao.selectOfficeIdList();
		for (Long officeId:officeIdList
				) {
			officeDao.generateToken(officeId, UuidUtil.getInstance().getUUid());
		}
	}

	/**
	 * 
	 *
	 * @Function 校验新增权限
	 * @FileName OfficeServiceImpl.java
	 * @MethodName checkAddPermission
	 * @param params
	 * @return
	 * @Date 2018年11月27日 下午2:30:56
	 * @Author Min DongXv
	 *
	 */
	@Override
	public int checkAddPermission(Map<String, Object> params) {
		return officeDao.checkAddPermission(params);
	}

	/**
	 * 查询已删除的部门
	 *
	 * @return 已删除的部门id
	 * @author YaoS
	 * @date 19/5/27 14:38
	 */
	@Override
	public List<String> deleteDept() {
		return officeDao.deleteDept();
	}
	
	@Override
	public Long getDeptIdByName(String deptName) {
		return officeDao.getDeptIdByName(deptName);
	}


}